/**
 * @file Main.as
 * 太极地幕主类
 *  
 * @author db0@qq.com
 * @version 1.0.1
 * @date 2017-09-08
 */
package
{
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.display.StageDisplayState;
	import flash.display.StageScaleMode;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.events.TouchEvent;
	import flash.text.AntiAliasType;
	import flash.text.TextField;
	import flash.text.TextFormat;
	import flash.ui.Multitouch;
	import flash.ui.MultitouchInputMode;
	import flash.utils.clearTimeout;
	import flash.utils.setTimeout;

	[SWF(width="1024",height="1024",frameRate="12",backgroundColor=0x000000)] public class Main extends Sprite
	{
		private var maxTime:uint = 1;//1秒内,同时踩中红蓝点,出闪电特效

		public static var stageW:int;
		public static var stageH:int;
		public static var whiteArr:Array;
		public static var blackArr:Array;
		public static var redArr:Array;
		public static var blueArr:Array;
		public static var bothArr:Array;
		public static var screenSaveArr:Array;
		public static var main:Main;
		public function Main()
		{
			main = this;
			whiteArr = SwfLoader.filesInDir("asset/ink/",SwfLoader.imgReg);
			blackArr = SwfLoader.filesInDir("asset/flowers/",SwfLoader.imgReg);
			/*
			   blueArr = SwfLoader.filesInDir("asset/单个太极眼/",SwfLoader.imgReg);
			   redArr = SwfLoader.filesInDir("asset/yellow/",SwfLoader.imgReg);
			   if(redArr==null)
			   redArr = blueArr;
			   screenSaveArr = SwfLoader.filesInDir("asset/地幕屏保/",SwfLoader.imgReg);
			   bothArr = SwfLoader.filesInDir("asset/太极眼闪电/",SwfLoader.imgReg);
			 */

			blueArr = SwfLoader.filesInDir("asset/red/",SwfLoader.imgReg);
			redArr = SwfLoader.filesInDir("asset/yellow/",SwfLoader.imgReg);
			if(redArr==null)
				redArr = blueArr;
			screenSaveArr = SwfLoader.filesInDir("asset/screensave/",SwfLoader.imgReg);
			bothArr = SwfLoader.filesInDir("asset/guang/",SwfLoader.imgReg);

			CONFIG::debugging{
				//addChild(new logs);
				logs.adds(whiteArr);
				logs.adds(blackArr);
				logs.adds(bothArr);
			}

			SwfLoader.SwfLoad("asset/taiji.png",loaded);

		}

		private function loaded2(e:Event):void
		{
			var bmp:Bitmap = null;
			if(e && Event.COMPLETE == e.type)
			{
				bmp = e.target.content as Bitmap;
			}

			if(bmp){
				bmp.width = stageW;
				bmp.height = stageH;
				addChild(bmp);
			}
		}

		private var bg:Bitmap;
		private function loaded(e:Event):void
		{
			stageW = stage.stageWidth;
			stageH = stage.stageHeight;
			stage.scaleMode = StageScaleMode.EXACT_FIT;
			stage.displayState = StageDisplayState.FULL_SCREEN;

			SwfLoader.SwfLoad("taiji.png",loaded2);

			if(e && Event.COMPLETE == e.type)
			{
				bg = e.target.content as Bitmap;
			}

			/*
			if(bg){
				bg.width = stageW;
				bg.height = stageH;
				//addChild(bg);
			}
			*/

			//logs.adds(Multitouch.supportsTouchEvents);
			if(Multitouch.supportsTouchEvents){
				var touchObj:MultiTouch = new MultiTouch();
				addChild(touchObj);
				//touchObj.addEventListener(TouchEvent.TOUCH_BEGIN, onTouchBegin);
			}else{
				stage.addEventListener(MouseEvent.MOUSE_DOWN,onTouchBegin);
			}

			stage.addEventListener(MouseEvent.MOUSE_UP,resetClock);
			stage.addEventListener(MouseEvent.MOUSE_DOWN,resetClock);
			stage.addEventListener(MouseEvent.MOUSE_MOVE,resetClock);

			screenSave();
		}

		private var timeoutId:uint;
		private function resetClock(e:MouseEvent):void
		{
			clearTimeout(timeoutId);
			timeoutId = setTimeout(screenSave,30*1000);//30s 启动屏保

			if(screenSavePlayer)
			{
				if(screenSavePlayer.parent)
					screenSavePlayer.parent.removeChild(screenSavePlayer);
				ViewSet.removes(screenSavePlayer);
				screenSavePlayer = null;
			}
		}

		private var screenSavePlayer:SequencePlayer;
		private function screenSave():void
		{
			if(screenSavePlayer==null)
			{
				screenSavePlayer = new SequencePlayer(screenSaveArr);
				screenSavePlayer.setSize(1024,1024);
				screenSavePlayer.x = 0;
				screenSavePlayer.y = 0;
			}
			stage.addChild(screenSavePlayer);
		}


		private function getColor(_x:int,_y:int):String
		{
			//var pixel:uint = bg.bitmapData.getPixel32(mouseX,mouseY);
			var pixel:uint = bg.bitmapData.getPixel(_x,_y);
			var r:int = (pixel>>16)&0xff;
			var g:int = (pixel>>8)&0xff;
			var b:int = (pixel)&0xff;
			//logs.adds("0x"+pixel.toString(16),r.toString(16),g.toString(16),b.toString(16));
			var s:String = "";
			if(r>0xcc && g<0x33 && b<0x33){
				s = "red";
			}else if(r<0x33 && g<0x33 && b<0x33){
				s = "black";
			}else if(r>0xcc && g>0xcc && b>0xcc){
				s = "white";
			}else if(r<0x33 && g<0x33 && b>0xcc){
				s = "blue";
			}
			logs.adds(s);
			return s;
		}


		private function onTouchBegin(e:MouseEvent):void
		{
			downPoint(e.stageX,e.stageY);
		}

		private var onRed:Boolean = false;
		private var onBlue:Boolean = false;
		private var lighting:Boolean = false;

		private var lastRedEyeTime:uint;
		private var lastBlueEyeTime:uint;

		public function downPoint(_x:int,_y:int):void
		{
			var scales:Number = 3/5;
			var picH:int = 270*scales;
			var s:String = getColor(_x,_y);
			switch(s)
			{
				case "red":
					lastRedEyeTime = uint(new Date());
					onRed = true;
					if(lastRedEyeTime -lastBlueEyeTime>maxTime*1000)
						onBlue = false;
					if(Multitouch.supportsTouchEvents){
						onBlue = hasColorPointArr("blue");
					}
					if(onBlue && lighting==false){
						playArr(bothArr);
					}else{
						playArr(redArr,_x,_y);
					}
					break;
				case "blue":
					lastBlueEyeTime = uint(new Date());
					onBlue= true;
					if(lastBlueEyeTime-lastRedEyeTime>maxTime*1000)
						onRed = false;
					if(Multitouch.supportsTouchEvents){
						onRed = hasColorPointArr("red");
					}
					if(onRed && lighting==false){
						playArr(bothArr);
					}else{
						playArr(blueArr,_x,_y);
					}
					break;

				case "white":
					playArr(whiteArr,_x,_y);
					break;
				case "black":
					playArr(blackArr,_x,_y);
					break;
			}
		}

		public function movePoint(o:Object):void
		{
			if(Multitouch.supportsTouchEvents){
				onBlue = hasColorPointArr("blue");
				onRed = hasColorPointArr("red");
			}
			if(onRed && onBlue && lighting==false)
				playArr(bothArr);
		}

		private function hasColorPointArr(_color:String,arr:Array=null):Boolean
		{
			if(arr==null)
			{
				arr = MultiTouch.points;
			}
			for each (var p:Object in arr)
			{
				var s:String = getColor(p.x,p.y);
				if(s==_color)
					return true;
			}
			return false;
		}


		private function playArr(arr:Array,_x:int=0,_y:int=0):void
		{
			if(arr==bothArr)
				lighting = true;
			var scales:Number = .48;
			var _w:int = 480*scales;
			var _h:int = 270*scales;

			var player:SequencePlayer = new SequencePlayer(arr);
			if(arr == blueArr || arr==redArr){
				_w = _h = 300;
				_x = stageW/2;
				if(_y>stageH/2){
					_y = 752;
				}else{
					_y = 272;
				}
			}else if(lighting && arr==bothArr){
				_w = stageW;
				_h = stageH;
				_x = _w/2;
				_y = _h/2;
			}
			player.setSize(_w,_h);
			player.x = _x-_w/2;
			player.y = _y-_h/2;

			if(lighting && arr!=bothArr)
				addChildAt(player,numChildren-2);
			else
				addChild(player);

			var o:Object = {target:player,id:0};
			if(arr==bothArr){
				o.id = setTimeout(removeTarget,10000,o);
			}else{
				o.id = setTimeout(removeTarget,4000,o);
			}
		}
		private function removeTarget(o:Object):void
		{
			if(o.target.width==1024)
			{
				lighting = false;
				onRed = false;
				onBlue = false;
			}

			clearTimeout(o.id);
			removeChild(o.target);
			ViewSet.removes(o.target);
		}
	}
}
